package com.example.studentalarm.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;

import com.example.studentalarm.presenter.BasePresenter;
import com.example.studentalarm.service.AlarmService;
import com.example.studentalarm.utils.Utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * 模型
 */
public abstract class BaseModelActivity<V, T extends BasePresenter<V>> extends AppCompatActivity {
    public abstract int getLayoutUrl();//获得布局地址

    public abstract void registerById();//注册ID

    public abstract void initData();//初始化页面数据

    public T presenter;
    public boolean isFirstInit = true;

    // 实例化presenter
    protected abstract T initPresenter();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
        }
        super.onCreate(savedInstanceState);
        setContentView(getLayoutUrl());
        presenter = initPresenter();
        //绑定VIew
        registerById();
    }

    private boolean fixOrientation() {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }

    public void gotoActivity(Class<? extends Activity> activityClass) {
        Intent intent = new Intent(this, activityClass);
        startActivity(intent);
    }

    public void gotoActivity(Class<? extends Activity> activityClass, Bundle bundle) {
        Intent intent = new Intent(this, activityClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    protected void gotoActivityForResult(Class<? extends Activity> activityClass, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, activityClass);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    protected void gotoActivitySetResult(Class<? extends Activity> activityClass, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, activityClass);
        intent.putExtras(bundle);
        setResult(requestCode, intent);
        finishActivity();
    }

    /***
     * 闹钟提醒操作
     */
    public void startAlarmService() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setPackage("com.example.studentalarm");
        intent.setClass(this, AlarmService.class);
        //对8.0系统兼容问题处理，服务放置前台服务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent);
        } else {
            startService(intent);
        }
    }

    /**
     * 重写关闭Activity
     **/
    public void finishActivity() {
        finish();
        Utils.closeSoftKeyboard(this);
    }


    /**
     * TOUCH关闭软键盘
     **/
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Utils.closeSoftKeyboard(this);
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (isFirstInit) {
            isFirstInit = false;
            initData();
        }
    }

    @Override
    protected void onDestroy() {
        presenter.detachView();
        super.onDestroy();

    }

}
